package club.justwill.single.persistence.jpo;

import club.justwill.single.persistence.support.OrdersBase;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by luoyu on 15-12-24.
 */

@Entity
@Table(name = "orders")
@NamedQueries({
        @NamedQuery(name="order.getByUserId",
                query="select orders from Orders orders where orders.userId =:userId"),
        @NamedQuery(name="order.getByOrderId",
                query="select orders from Orders orders where orders.orderId = :orderId"),
})
public class Orders extends OrdersBase {

    @Id
    @Column(name = "order_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long orderId;

    //一个用户可以有多个订单，订单跟用户是一对多关系，多方
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    private User user;

    //订单跟OrderItem是一对多关系 一方
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "orders")
    private Set<OrderItem> orderItemSet;

    public Set<OrderItem> getOrderItemSet() {
        if(orderItemSet == null){
            this.orderItemSet = new HashSet<OrderItem>();
        }
        return orderItemSet;
    }

    public void setOrderItemSet(Set<OrderItem> orderItemSet) {
        this.orderItemSet = orderItemSet;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        if(orderId == null) {
            this.orderId = 0;
        } else {
            this.orderId = orderId;
        }
    }

}
